import INavigate from "../../Interfaces/INavigate";
import Oriented from "../../Constants/Oriented";
import G from "../../Utilities/Global";
import Constants from "../../Constants/Constants";

/**
 *  贴边寻路
 *   ------------
 *   |          |
 *   |          |
 *   |         _|
 *   |        |_|
 *   |          |
 *   ------------
 * created by Five on 2018-11-20 18:37:27
 */
export default class PasteWall implements INavigate {
    speed: number;
    setspeed(sp: number) {
        this.speed = sp;
    }
    oriented: any;
    setdir(dir: any) {
        this.oriented = dir;
        if (dir == Oriented.LEFT)
            this.setClock(1)
        else
            this.setClock(0)
    }

    // 贴墙需要一个宽高
    size: number;
    setsize(s: number) {
        this.size = s;
    }

    // 1 逆时针 0 顺时针
    clockDir: number;
    setClock(c: number) {
        this.clockDir = c;
    }

    target: cc.Vec2 = cc.Vec2.ZERO;
    settarget(node: cc.Node) {
        if (this.target.mag() > 5)
            return;
        this.predict(node.position);
        this.target = this.calSpeed(1);
    }

    update(dt: number, node: cc.Node, aniNode: cc.Node) {
        if (this.target.mag() < 1)
            return;
        let target = this.calSpeed(dt);
        this.target.subSelf(target);
        let pos = node.position.add(target);
        node.position = pos;
        aniNode.is3DNode = true;
        if (this.oriented == Oriented.RIGHT)
            aniNode.eulerAngles = Constants.OppsiteRotate;
        if (this.oriented == Oriented.LEFT)
            aniNode.eulerAngles = cc.Vec3.ZERO;
    }

    predict(current: cc.Vec2) {
        current.addSelf(cc.v2(5, 5));
        let loc = G().Calc.PositionToLocation(current);
        if (!G().AStar.has(loc)) return;
        let area = G().AStar.get(loc);

        if (this.clockDir == 1) {
            // 逆
            if (this.oriented == Oriented.UP) {
                if ((area & Oriented.RIGHT) > 0)
                    this.oriented = Oriented.RIGHT;
                else if ((area & Oriented.UP) > 0)
                    this.oriented = Oriented.UP;
                else if ((area & Oriented.LEFT) > 0)
                    this.oriented = Oriented.LEFT;
                else if ((area & Oriented.DOWN) > 0)
                    this.oriented = Oriented.DOWN;
            } else if (this.oriented == Oriented.LEFT) {
                if ((area & Oriented.UP) > 0)
                    this.oriented = Oriented.UP;
                else if ((area & Oriented.LEFT) > 0)
                    this.oriented = Oriented.LEFT;
                else if ((area & Oriented.DOWN) > 0)
                    this.oriented = Oriented.DOWN;
                else if ((area & Oriented.RIGHT) > 0)
                    this.oriented = Oriented.RIGHT;
            } else if (this.oriented == Oriented.DOWN) {
                if ((area & Oriented.LEFT) > 0)
                    this.oriented = Oriented.LEFT;
                else if ((area & Oriented.DOWN) > 0)
                    this.oriented = Oriented.DOWN;
                else if ((area & Oriented.RIGHT) > 0)
                    this.oriented = Oriented.RIGHT;
                else if ((area & Oriented.UP) > 0)
                    this.oriented = Oriented.UP;
            } else if (this.oriented == Oriented.RIGHT) {
                if ((area & Oriented.DOWN) > 0)
                    this.oriented = Oriented.DOWN;
                else if ((area & Oriented.RIGHT) > 0)
                    this.oriented = Oriented.RIGHT;
                else if ((area & Oriented.UP) > 0)
                    this.oriented = Oriented.UP;
                else if ((area & Oriented.LEFT) > 0)
                    this.oriented = Oriented.LEFT;
            }
        } else {
            // 顺
            if (this.oriented == Oriented.UP) {
                if ((area & Oriented.LEFT) > 0)
                    this.oriented = Oriented.LEFT;
                else if ((area & Oriented.UP) > 0)
                    this.oriented = Oriented.UP;
                else if ((area & Oriented.RIGHT) > 0)
                    this.oriented = Oriented.RIGHT;
                else if ((area & Oriented.DOWN) > 0)
                    this.oriented = Oriented.DOWN;
            } else if (this.oriented == Oriented.LEFT) {
                if ((area & Oriented.DOWN) > 0)
                    this.oriented = Oriented.DOWN;
                else if ((area & Oriented.LEFT) > 0)
                    this.oriented = Oriented.LEFT;
                else if ((area & Oriented.UP) > 0)
                    this.oriented = Oriented.UP;
                else if ((area & Oriented.RIGHT) > 0)
                    this.oriented = Oriented.RIGHT;
            } else if (this.oriented == Oriented.DOWN) {
                if ((area & Oriented.RIGHT) > 0)
                    this.oriented = Oriented.RIGHT;
                else if ((area & Oriented.DOWN) > 0)
                    this.oriented = Oriented.DOWN;
                else if ((area & Oriented.LEFT) > 0)
                    this.oriented = Oriented.LEFT;
                else if ((area & Oriented.UP) > 0)
                    this.oriented = Oriented.UP;
            } else if (this.oriented == Oriented.RIGHT) {
                if ((area & Oriented.UP) > 0)
                    this.oriented = Oriented.UP;
                else if ((area & Oriented.RIGHT) > 0)
                    this.oriented = Oriented.RIGHT;
                else if ((area & Oriented.DOWN) > 0)
                    this.oriented = Oriented.DOWN;
                else if ((area & Oriented.LEFT) > 0)
                    this.oriented = Oriented.LEFT;
            }
        }
    }

    calSpeed(dt) {
        switch (this.oriented) {
            case Oriented.RIGHT:
                return cc.v2(this.speed * dt, 0);
            case Oriented.UP:
                return cc.v2(0, this.speed * dt);
            case Oriented.LEFT:
                return cc.v2(-this.speed * dt, 0);
            case Oriented.DOWN:
                return cc.v2(0, -this.speed * dt);
            default:
                return cc.Vec2.ZERO;
        }
    }
}